define([ 
         'jquery',
         'Views/memoView', 
         'Models/Folder', 
         'Models/Memo'

],
		function($, MemoView, Folder, Memo) {

			var MemoController = function(ajaxController, mainController) {

				this.ajaxController = ajaxController;
				this.mainController = mainController;

				var memo = new Memo();
				var memoView = new MemoView();
				var _this = this;
				var event1 = null;
				var event2 = null;
				var event3 = null;
				var removeBtnEvent = null;
				this.memo = {};

				this.drawing = function(res) {

					memoView.drawing(res); // 전체 화면 뿌려주기
					this.getFolderList();

					memoView.setUpdateEvent(function() { // 회원 수정 폼 버튼

						_this.mainController.router("update");

					});
					memoView.setLogoutEvent(function() { // 로그아웃 버튼

						_this.mainController.router("logout");

					});
					memoView.setRemoveEvent(function() { // 회원 탈퇴 버튼
						_this.mainController.router("delete");
					});
					memoView.setCreateFolderEvent(function() { // 폴더 생성 버튼

						_this.addFolder(res);

					});
					memoView.setAllFolderEvent(function() { // 전체 메모 버튼
						
						$('.folderclick').removeClass("folderclick");
						$('#allFolder').addClass("folderclick");
						var $createMemo =  $('#createMemo');
						$createMemo.attr("folder_data", null);
						_this.getMemoList(null, "전체");
						

					});
					memoView.setCreateMemoEvent(function() { // 메모 생성 버튼

						_this.addMemo();

					});
					memoView.setResetEvent(function() { // 메모 리셋 버튼

						_this.resetMemo();

					});
					memoView.setSaveMemoEvent(function() { // 메모 저장 버튼

						_this.saveMemo();

					});

				};

				this.addMemo = function() { // 메모 저장

					var params = memoView.addMemo();
					if(params == null) {
						alert("폴더를 먼저 선택해주세요!");
					}else {
						this.ajaxController.getData("POST", "./addMemo.do", params,_this.drawMemo, "memo");
					}
					
	
				};
				this.drawMemo = function(memos) { // 메모 그려줌
				
					if(memos instanceof Array) {
						for( var i = 0 ; i < memos.length; i++) {
							
							var memoId = memos[i].memo_id;
							var memoContent = memos[i].memo_content;
							this.memo[memoId] = new Memo(memoId,memoContent);
								
							var removeBtnEvent = function(e) {
								var MemoBtn = e.target;
								var MemoNum = $(MemoBtn).attr("dataTarget");
								
								var params = "id="+MemoNum;
								
								_this.ajaxController.contact("POST","./removeMemo.do", params);
								
								MemoBtn.remove();
								$('.click').remove();  
								
							};
	
							
							memoView.drawMemo(this.memo[memoId], removeBtnEvent);
					
							
						}
					}
				};


				this.resetMemo = function() { // 메모 리셋 버튼

					memoView.resetMemo();

				};

				this.saveMemo = function() { // 메모 저장 버튼
					
					var params = null;
					if( $('#text').val() != "" ) {
						
						var memo = $('#text').val();
						var memoID = $('#text').attr("memo_id");
						params = "id="+memoID+"&content="+memo;  
						
						if(memoID === undefined || memoID === null) {
							alert('저장할 메모 탭을 먼저 클릭하세요!');
						}else {
							
							if(memo.split("\n")[0].length < 10) {
								$('.click').val(memo.split("\n")[0]);

							}else {
								$('.click').val(memo.slice(0,10));
					
							}
						}
						
						this.memo[memoID] = new Memo(memoID, memo);
						this.ajaxController.getData("POST", "./updateMemo.do",params, /*_this.setMemoContent*/memoView.addText, "memoContent");
						
					} else {
						alert('메모 내용을 입력해주세요!');
					}
					

				};
			
				this.getMemoList = function(folderNum, folderName) { // 메모

					if (folderName == "전체" || folderNum == null) {

						var params = "folderName=" + folderName + "&folderNum=" + folderNum;
						var $memoArticle = $('#memoArticle');
						$memoArticle.html("");
						this.ajaxController.getData("POST", "./getMemoList.do",params, this.drawMemo, "memo");

					} else {
						
						var params = "folderName=" + folderName + "&folderNum="+ folderNum;
						var $memoArticle = $('#memoArticle');
						$memoArticle.html("");
						this.ajaxController.getData("POST", "./getMemoList.do",params, _this.drawMemo, "memo");
					}

				};
				///////////////////////////////////// 폴더 관리

				

				this.drawFolder = function(folders) { // 폴더리스트 뿌려주기
		

					if(folders instanceof Array) {
						for( var i = 0 ; i < folders.length; i++) {
							
							var id = folders[i].id;
							var folderNum = folders[i].folder_num;
							var folderName = folders[i].folder_name;
							var folder = new Folder(id, folderName, folderNum);

							 removeFolderBtnEvent = function(e) {
								var folderBtn = e.target;
								var folderNum = $(folderBtn).attr("dataTarget");
								
								var params = "id="+folderNum;
								
								_this.ajaxController.contact("POST","./removeFolder.do", params);
								
								folderBtn.remove();
								$('.folderclick').remove();  
								
							};
							
							event1 = function(e) {//////////////////////////undefined
								var folderBtn = e.target;
								console.dir(folder.getFolderNum());
								
								memoView.selectFolder(folderBtn, removeFolderBtnEvent);
								_this.getMemoList(folderBtn.id, folderBtn.value);
								var $createMemo =  $('#createMemo');
								$createMemo.attr("folder_data", folderBtn.id);
								
							};
							event2 = function(e) {//////////////////////////
								
								var folderBtn = e.target;
								if ($(folderBtn).attr("type") == "button") {
									
									$(folderBtn).attr("type", "text");      
								}
								
								
							};
							event3 = function(e) {////////////////////////////
								var folderBtn = e.target;
								var $folder = $(folderBtn);
								
								if ($folder.attr("type") == "text") {
									
									if(folderBtn.value.length > 10 ) {
										alert('폴더 제목은 10글자만 가능합니다.');
										
									}else {
										var params = "folderNum="+folderBtn.id+"&folderName="+folderBtn.value;  
										_this.ajaxController.contact("POST","./updateFolder.do", params);
										$folder.attr("type", "button");   
									}
			
								}
								
							};
	
							memoView.addFolder(null,folder, event1,event2, event3, null);
						}
					}
					
					
					
					
				};
				this.addFolder = function(id) {              //새로운 폴더 삭제 버튼 만들기

					
					var event4 = function(e) {
						
						var folderBtn = e.target;
						var params = "id=" + id + "&foldername="+ folderBtn.value;
						
						_this.ajaxController.getData("POST", "./addFolder.do",params, _this.setFolderNum , "folderNum");  //포커스 아웃되면 계속 생성
						
						$(folderBtn).attr("type", "button");
						memoView.addFolder(true,null,event1,event2,event3);
	
						
					};
					
					memoView.addFolder(null,null,null,null,null,event4);
							
				};
				
				this.getFolderList = function() { // 폴더 리스트 가져오기 
					var $folderArticle = $('#folderArticle');
					$folderArticle.html("");
					
					this.ajaxController.getData("POST", "./getFolderList.do",null, this.drawFolder, "folder");

				};
				this.setFolderNum = function(folder) {
					
					var id = folder.id;
					var name = folder.folder_name;
					var num = folder.folder_num;
					
					var folder = new Folder(id, name, num);
					$('#newFolder').attr("id",num);
				};
				this.setMemoContent = function(memo) {
					
					var num = memo.memo_id;
					var content = memo.memo_content;
					
					var memo = new Memo(num, content);
					console.log(content);
					$('#text').val(content);
				
				};

			};
	
			return MemoController;

		});
